package com.ffcs.cn.action.map;


import com.ffcs.cn.DevelopProperties;
import com.ffcs.cn.action.common.AbstractPanelForm;
import com.ffcs.cn.action.common.FormModel;
import com.supermap.desktop.controls.ui.controls.DialogResult;
import com.supermap.desktop.controls.ui.controls.datasetChoose.DatasetChooseMode;
import com.supermap.desktop.controls.ui.controls.datasetChoose.SmDatasetChooser;
import com.supermap.desktop.controls.utilities.MapViewUIUtilities;
import com.supermap.desktop.core.Application;
import com.supermap.desktop.core.Interface.IBaseItem;
import com.supermap.desktop.core.implement.CtrlAction;
import com.supermap.desktop.core.utilties.TableUtilities;
import com.supermap.mapping.Map;

import java.awt.event.ActionListener;

/**
 * 打开地图
 *
  */
public class CtrlActionOpenMap extends CtrlAction {
	public CtrlActionOpenMap(IBaseItem caller) {
		super(caller);
	}

	@Override
	protected void run() {
		new DialogOpenMap().showDialog();
	}

	/**
	 * 选择地图面板
	 */
	private static class DialogOpenMap extends AbstractPanelForm {

		public DialogOpenMap() {
			setTitle(DevelopProperties.getString("String_Title_OpenMap"));
		}

		@Override
		public FormModel getTableModel() {
			return new FormModel() {
				private final String[] columnNames = {DevelopProperties.getString("String_MapName")};

				@Override
				public Object getValueAt(int row, int column) {
					Object bean = get(row);
					if (column == 0) {
						return (bean);
					}
					return null;
				}

				@Override
				public int getColumnCount() {
					return columnNames.length;
				}

				@Override
				public String getColumnName(int column) {
					return columnNames[column];
				}

				@Override
				public Class<?> getColumnClass(int columnIndex) {
					return Map.class;
				}
			};
		}

		/**
		 * 点击添加按钮，从地图选择界面选择将会被打开的地图
		 *
		 * @return 点击添加按钮后触发事件的监听器
		 */
		@Override
		public ActionListener getAddActionListener() {
			return e -> {
				SmDatasetChooser smDatasetChooser = new SmDatasetChooser(DatasetChooseMode.MAP);
				if (smDatasetChooser.showDialog() == DialogResult.OK) {
					java.util.List<Object> selectedObjects = smDatasetChooser.getSelectedObjects();
					for (Object obj : selectedObjects) {
						if (obj instanceof Map && !tableModel.contains(obj)) {
							int rowCount = tableModel.getRowCount();
							tableModel.add(obj);
							TableUtilities.setTableSelectedRows(getItemTable(), rowCount);
						}
					}
				}
				smDatasetChooser.dispose();
			};
		}

		/**
		 * 监听确定按钮，点击后批量打开选中的地图
		 *
		 * @return 点击确认按钮后触发事件的监听器
		 */
		@Override
		public ActionListener getOkActionListener() {
			return e -> {
				try {
					// 获取选择的数据集
					Object[] data = tableModel.getData().toArray();
					for (Object datum : data) {
						if (datum instanceof Map) {
							// 根据名称依次打开地图
							MapViewUIUtilities.openMap(((Map) datum).getName());
						}
					}
				} catch (Exception exception) {
					Application.getActiveApplication().getOutput().output(exception);
				} finally {
					this.dispose();
				}
			};
		}
	}
}
